﻿/*
 * Last review by   : Yaser
 * Last review date : JUL-25-09 
 */
using System;
using ERP.Framework.Components.LoggingHelper.ErrorLog;
using ERP.Framework.Components.Utilities.TracingHelper;

namespace ERP.Framework.Components.ExceptionHandler
{
    public abstract class ExceptionPublisher
    {
        private const string TRACING_CATEGORY = "ERP.Framework.Components.ExceptionHandler.ExceptionPublisher";

        #region Methods

        /// <summary>
        /// publish the exception to the event log
        /// </summary>
        /// <param name="ex">exception</param>
        public static void PublishException(Exception ex)
        {
            PublishException(ex,
                ((ErrorLogType)ConfigurationHelper.ConfigurationManager.ExceptionHandlerSettings.ErrorLogType)
                , ConfigurationHelper.ConfigurationManager.ExceptionHandlerSettings.ErrorLogFilePath
                , ConfigurationHelper.ConfigurationManager.ExceptionHandlerSettings.ErrorLogName
                );
        }

        /// <summary>
        /// publish the exception to the event log
        /// </summary>
        /// <param name="ex">exception</param>
        /// <param name="errorLogType">type</param>
        /// <param name="errorLogFilePath">xml file path</param>
        public static void PublishException(Exception ex, ErrorLogType errorLogType, string errorLogFilePath, string errorLogName)
        {
            TraceUtil.Write(TRACING_CATEGORY, "PublishException : Start");
            IErrorLog log;
            switch (errorLogType)
            {
                case ErrorLogType.Windows:
                    TraceUtil.Write(TRACING_CATEGORY, string.Format( "PublishException : Windows Log : {0}", errorLogName));
                    log = new WindowsErrorLog(errorLogName);
                    log.Add(ex);
                    break;
                case ErrorLogType.XML:
                    TraceUtil.Write(TRACING_CATEGORY, string.Format("PublishException : custom XML Log : {0}", errorLogFilePath));
                    log = new XMLErrorLog(errorLogFilePath);
                    log.Add(ex);
                    break;
            }
            TraceUtil.Write(TRACING_CATEGORY, "PublishException : End");
        }
        #endregion
    }
}
